#!/usr/bin/env python
# -*- coding=utf-8 -*-

import pygame


class Ship:
    """初始化飞船并设置其初始位置"""

    def __init__(self, ai_settings, screen):
        self.screen = screen
        self.ai_settings = ai_settings
        self.image = pygame.image.load("images/ship.bmp")
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()
        self.rect.centerx = self.screen_rect.centerx
        self.rect.centery = self.screen_rect.centery
        self.rect.bottom = self.screen_rect.bottom

        self.local_x = float(self.rect.centerx)
        self.local_y = float(self.rect.centery)
        # 移动标识
        self.moving_right = False
        self.moving_left = False
        self.moving_up = False
        self.moving_down = False

    def update(self):
        """
        根据标识移动飞船
        :return:
        """
        if self.moving_right and self.rect.right < self.screen_rect.right:
            self.local_x += self.ai_settings.ship_speed_factor
        if self.moving_left and self.rect.left > 0:
            self.local_x -= self.ai_settings.ship_speed_factor
        if self.moving_up and self.rect.top > 0:
            self.local_y -= self.ai_settings.ship_speed_factor
        if self.moving_down and self.rect.bottom < self.ai_settings.screen_height:
            self.local_y += self.ai_settings.ship_speed_factor
        self.rect.centerx = self.local_x
        self.rect.centery = self.local_y

    def blitme(self):
        self.screen.blit(self.image, self.rect)
